/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.contactv2.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.constant.Constants;
import com.jzwl.common.id.Sequence;
import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.dao.BaseDAO;



@Repository("siteContactV2Dao")
public class SiteContactV2Dao {
	
	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库
	
	@Autowired
	private Constants constants;
	
	public boolean addSiteContactV2(Map<String, Object> map) {
		
		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		map.put("id",  Sequence.nextId());
		map.put("createTime", new Date());
		
		String sql = "insert into " + constants.getCurrentDB() + "site_contact_v2 " 
				 + " (id,contype,name,content,createTime) " 
				 + " values "
				 + " (:id,:contype,:name,:content,:createTime)";
		
		return baseDAO.executeNamedCommand(sql, map);
	}
	
	
	public String getColumns() {
		return ""
				+" id as id,"
				+" name as name,contype as contype,"
				+" content as content,"
				+" createTime as createTime"
				;
	}
	

	public PageObject querySiteContactV2List(Map<String,Object> map) {
		// [column]为字符串拼接, {column}为使用占位符. 如username='[username]',偷懒时可以使用字符串拼接 
		// [column] 为PageRequest的属性
		
		String sql="select " + getColumns() + " from " + constants.getCurrentDB() + "site_contact_v2 t where 1=1 ";
		
		  	
		  		if(null !=map.get("name") && StringUtils.isNotEmpty(map.get("name").toString())){
				  		sql=sql+ " and t.name  = " + map.get("name") +"";
			  	}
		  	
		  		if(null !=map.get("content") && StringUtils.isNotEmpty(map.get("content").toString())){
				  		sql=sql+ " and t.content  = " + map.get("content") +"";
			  	}
		  	
		  		if(null !=map.get("createTimeBegin") && StringUtils.isNotEmpty(map.get("createTimeBegin").toString())){
	  				sql=sql+ " and t.createTime >= '" + map.get("createTimeBegin") +"'";
		  		}
		  		if(null !=map.get("createTimeEnd") && StringUtils.isNotEmpty(map.get("createTimeEnd").toString())){
	  				sql=sql+ " and t.createTime <= '" + map.get("createTimeEnd"  ) +"'";
		  		}
					
					
		sql=sql+ " order by id ";
		
		PageObject po = baseDAO.queryForMPageList(sql, new Object[]{},map);
		
		return po;
	}

	public boolean updateSiteContactV2(Map<String, Object> map) {
		
		String sql ="update " + constants.getCurrentDB() + "site_contact_v2 set ";
				  	if(null !=map.get("name") && StringUtils.isNotEmpty(map.get("name").toString())){
					  	sql=sql+ "			name=:name ,";
				  	}
				  	if(null !=map.get("content") && StringUtils.isNotEmpty(map.get("content").toString())){
					  	sql=sql+ "			content=:content ,";
				  	}
				  	if(null !=map.get("createTime") && StringUtils.isNotEmpty(map.get("createTime").toString())){
					  	sql=sql+ "			createTime=:createTime ,";
				  	}
				  	if(null !=map.get("contype") && StringUtils.isNotEmpty(map.get("contype").toString())){
				  		sql=sql+ "			contype=:contype ,";
				  	}
				sql = sql.substring(0, sql.length()-1);
				sql = sql + " where id=:id";
		return  baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteSiteContactV2(Map<String, Object> map) {
		
		String sql = "delete from " + constants.getCurrentDB() + "site_contact_v2 where id in ("+ map.get("id")+ " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean delSiteContactV2(Map<String, Object> map) {
		
		String sql ="update " + constants.getCurrentDB() + "site_contact_v2 set "
				+ " isDelete =:"+map.get("isDelete")+" "
				+ " where id=:id";
		
		return  baseDAO.executeNamedCommand(sql, map);
	}
	
	
	@SuppressWarnings("unchecked")
	public Map<String,Object> getById(Map<String, Object> map) {
		
		String sql = "select " + getColumns() + " from " + constants.getCurrentDB() + "site_contact_v2 where id = "+ map.get("id") + "";
		
		return baseDAO.queryForMap(sql);
	}
	
	public List<Map<String,Object>> querySelectValue(Map<String,Object> map) {
		
		String sql = " SELECT dd.id,dd.dic_name "
				+ "    from dic_type  dt "
				+ "    LEFT JOIN  dic_data dd on dt.dic_id = dd.dic_id "
				+ "    where dt.dic_code = '"+map.get("code")+"'  and dd.isDelete = 0 ";
	 return	baseDAO.queryForList(sql);
	}
	

}
